package com.example.demo.delayQueue;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: cj
 * @create: 09-08-02 09:11:34
 */
public class MyDelay implements Delayed, Runnable {
    private long start;
    private long delayTime;

    public MyDelay(long delayTime) {
        this.start = System.currentTimeMillis ();
        this.delayTime = delayTime;//延时时长
    }

    @Override
    public int compareTo(Delayed o) {
        //用于队列中排序判断
        MyDelay notice = (MyDelay) o;
        return (int) (this.getDelay ( TimeUnit.MICROSECONDS ) - notice.getDelay ( TimeUnit.MICROSECONDS ));
    }

    @Override
    public long getDelay(TimeUnit unit) {
        //用于出队判断
        return unit.convert ( (start + delayTime) - System.currentTimeMillis (), TimeUnit.MICROSECONDS );
    }

    @Override
    public void run() {
        System.out.println ( "run-15" );
    }
}
